package org.oep;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Calendar;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;

public class Mingler extends Canvas {
	private Vector messageLog = new Vector();
	
	int currentEntry = 0;
	int currentDevice = 0;
	
	private JMingle parent;
	
	private Image antennaIcon;
	private Image persistentIcon;
	
	Font normal;
	Font small = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	
	Mingler(JMingle parent) {
		this.parent = parent;
		
		try {
			antennaIcon = Image.createImage("/res/icons/antenna.png");
		} catch (IOException e) {
			antennaIcon = null;
		}
		try {
			persistentIcon = Image.createImage("/res/icons/people.png");
		} catch (IOException e) {
			persistentIcon = null;
		}
	}
	
	/*
	 * Inherited from Canvas
	 */
	//--------------------------------------------------------------//
	public void keyPressed(int keyCode) {
		int gameCode = this.getGameAction(keyCode);
		switch(gameCode) {
		case Canvas.UP: this.scrollLogUp(); break;
		case Canvas.DOWN: this.scrollLogDown(); break;
		case Canvas.RIGHT: this.scrollDeviceRight(); break;
		case Canvas.LEFT: this.scrollDeviceLeft(); break;
		}
		this.repaint();
	}
	
	public void keyRepeated(int keyCode) {
		keyPressed(keyCode);
	}
	
	protected void paint(Graphics g) {
		if(normal == null) normal = g.getFont();
		
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(0);
		
		
		g.setFont(small);
		
		// Top-left: Draw the memory usage
		Runtime rt = Runtime.getRuntime();
		int pct = new Float(100 * (rt.totalMemory() - rt.freeMemory()) / (rt.totalMemory())).intValue();
		
		g.drawString("P: " + pct + "% T: " + rt.totalMemory() / 1000 + 'K', 0, 0, Graphics.TOP|Graphics.LEFT);
		
		// Top-right: Draw the busy icon and scan icon
		if(antennaIcon == null || persistentIcon == null) {
			String mode = new String();
			if(parent.persistentScanActive()) mode += "[p]";
			if(parent.antennaIsBusy()) mode += "[a]";
			if(mode.length() > 0)
				g.drawString(mode, this.getWidth(), 0, Graphics.TOP | Graphics.RIGHT);
		}
		
		else {
			if(parent.antennaIsBusy()) {
				g.drawImage(antennaIcon, this.getWidth(), 0, Graphics.TOP | Graphics.RIGHT);
			}
			if(parent.persistentScanActive()) {
				g.drawImage(persistentIcon, this.getWidth() - antennaIcon.getWidth(), 0, Graphics.TOP | Graphics.RIGHT);
			}
		}
		
		
		// Message log
		if(messageLog.size() > 0) {
			// Height of font.
			int px = g.getFont().getHeight();
			// Initial position on screen (each message moves up by px)
			int pos = (int)(this.getHeight() * 0.85);
			String msg;
			
			for(int i = currentEntry; i >= 0; i--) {
				// Calculate y-position of this entry.
				pos -= px;
				
				// Prevent messages from going within the icon row
				if( pos < ( 12 + px  ) ) {
					break;
				}
				
//				msg = (messageLog.elementAt( i ) != "" ? ( i + 1 ) + ". " + (String) messageLog.elementAt(i) : "" );
				msg = (String) messageLog.elementAt(i);
				g.drawString(msg, 0, pos, Graphics.LEFT | Graphics.BOTTOM);
			}
		}
		
		// Bottom: Device browse
		if(parent.numberOfDevices() > 0) {
			Enumeration nodes = parent.getDevices();
			MingleNode n = null;
			
			for(int i = 0; i < parent.numberOfDevices(); i++) {
				n = (MingleNode) nodes.nextElement();
				if(i == currentDevice) {
					break;
				}
			}
			
			String out = new String();
			if(currentDevice > 0) out += "< ";
			if(n != null)
				out += n.getName();
			else
				out += "[NULL]";
			if(currentDevice < parent.numberOfDevices() - 1) out += " >";
			g.drawString(out, this.getWidth() / 2, this.getHeight() - small.getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
		}
		String pos = new String();
		if(parent.numberOfDevices() > 0)
			pos += (currentDevice + 1);
		else
			pos += "0";
		
		String countLine = pos + "/" + parent.numberOfDevices(); 
		g.setFont(small);
		g.drawString(countLine, this.getWidth() / 2, this.getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
		
	}
	
	/*
	 * Internal functions
	 */
	//--------------------------------------------------------------//
	
	public void scrollLogUp() {
		if(currentEntry > 0) {
			currentEntry--;
		}
	}
	public void scrollLogDown() {
		if(currentEntry < messageLog.size() - 1) {
			currentEntry++;
		}
	}
	public void scrollDeviceLeft() {
		if(currentDevice > 0)
			currentDevice--;
	}
	public void scrollDeviceRight() {
		if(currentDevice < parent.numberOfDevices() - 1)
			currentDevice++;
	}

	
	/*
	 * External functions
	 */
	//--------------------------------------------------------------//
	public void showMessage(String message) {
		String hour, minute;
		if(currentEntry == messageLog.size() - 1) currentEntry++;
		Calendar calendar = Calendar.getInstance();
		int m = calendar.get(Calendar.MINUTE);
		int h = calendar.get(Calendar.HOUR_OF_DAY);
		
		if(m < 10) minute = "0" + m;
		else minute = String.valueOf(m);
		
		if(h < 10) hour = "0" + h;
		else hour = String.valueOf(h);
		
		messageLog.addElement(hour + ":" + minute + ": " + message);
		this.repaint();
	}
	
	public void clearMessageLog() {
		messageLog.removeAllElements();
		currentEntry = 0;
		this.repaint();
	}	
}
